﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//Author: Kha
namespace VietThuongLMS.Utils
{
    public class GeneralHTMLString
    {
        //with javascript function
        public static string HTMLForJavaScriptPagination(int totalItem,int itemPerPage, int curPage, string javascriptFunction, List<int> arguments)
        {
            int totalpage = totalItem / itemPerPage;
            if (totalItem % itemPerPage != 0)
                totalpage++;
            int start = curPage - curPage % itemPerPage + 1;
            int end = start + itemPerPage -1;
            if (end > totalpage)
                end = totalpage;
            string rs = "";
            for (int i = start; i <= end; i++)
            {
                rs = rs + "<li";
                if (i == curPage)
                    rs = rs + " class='disabled'>";
                else
                    rs = rs + ">";
                rs = rs + "<a href='javascript:" + javascriptFunction + "(";
                for (int j = 0; j < arguments.Count; j++)
                {
                    rs = rs + arguments[j];
                    if (j < arguments.Count - 1)
                        rs = rs + ",";
                    else
                    {
                        rs = rs + "," + i;
                    }
                }
                rs = rs + ");'>" + i.ToString() + "</a></li>";
            }

            if (end != totalpage)
            {
                rs = rs + "<li><a href='javascript:" + javascriptFunction + "(";
                for (int j = 0; j < arguments.Count; j++)
                {
                    rs = rs + arguments[j];
                    if (j < arguments.Count - 1)
                        rs = rs + ",";
                    else
                    {
                        rs = rs + "," + (end + 1);
                    }
                }
                rs = rs + ");'>Next</a></li>";
            }
            if (start != 1)
            {
                string temp = "<li><a href='javascript:" + javascriptFunction + "(";
                for (int j = 0; j < arguments.Count; j++)
                {
                    temp = temp + arguments[j];
                    if (j < arguments.Count - 1)
                        temp = temp + ",";
                    else
                    {
                        temp = temp + "," + (curPage - (curPage % itemPerPage) - itemPerPage + 1);
                    }
                }
                temp = temp + ");'>Previous</a></li>";
                rs = temp + rs;
            }
            if (start != 1)
            {
                rs = rs + "<li><a href='javascript:" + javascriptFunction + "(";
                for (int j = 0; j < arguments.Count; j++)
                {
                    rs = rs + arguments[j];
                    if (j < arguments.Count - 1)
                        rs = rs + ",";
                    else
                    {
                        rs = rs + "," + 1;
                    }
                }
                rs = rs + ");'>First</a></li>";
            }
            if (end != totalpage)
            {
                rs = rs + "<li><a href='javascript:" + javascriptFunction + "(";
                for (int j = 0; j < arguments.Count; j++)
                {
                    rs = rs + arguments[j];
                    if (j < arguments.Count - 1)
                        rs = rs + ",";
                    else
                    {
                        rs = rs + "," + totalpage;
                    }
                }
                rs = rs + ");'>End</a></li>";
            }
            rs = "<div class='pagination pagination-right'><ul>" + rs + "</ul></div>";
            return rs;
        }

        //non argunments javascript function
        public static string HTMLForJavaScriptPagination(int totalItem, int itemPerPage, int curPage, string javascriptFunction)
        {
            int totalpage = totalItem / itemPerPage;
            if (totalItem % itemPerPage != 0)
                totalpage++;
            int start = curPage - curPage % itemPerPage + 1;
            int end = start + itemPerPage - 1;
            if (end > totalpage)
                end = totalpage;
            string rs = "";
            for (int i = start; i <= end; i++)
            {
                rs = rs + "<li";
                if (i == curPage)
                    rs = rs + " class='disabled'>";
                else
                    rs = rs + ">";
                rs = rs + "<a href='javascript:" + javascriptFunction + "(";
                rs = rs + i;
                rs = rs + ");'>" + i.ToString() + "</a></li>";
            }

            if (end != totalpage)
            {
                rs = rs + "<li><a href='javascript:" + javascriptFunction + "(";
                rs = rs + (end + 1);
                rs = rs + ");'>Next</a></li>";
            }
            if (start != 1)
            {
                string temp = "<li><a href='javascript:" + javascriptFunction + "(";
                temp = temp + (curPage - (curPage % itemPerPage) - itemPerPage + 1);
                temp = temp + ");'>Previous</a></li>";
                rs = temp + rs;
            }
            if (start != 1)
            {
                rs = rs + "<li><a href='javascript:" + javascriptFunction + "(";
                rs = rs+ 1;
                rs = rs + ");'>First</a></li>";
            }
            if (end != totalpage)
            {
                rs = rs + "<li><a href='javascript:" + javascriptFunction + "(";
                rs = rs + totalpage;
                rs = rs + ");'>End</a></li>";
            }
            rs = "<div class='pagination pagination-right'><ul>" + rs + "</ul></div>";
            return rs;
        }
    }
}

